package com.ruoyi.service.production.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.production.MesProductionPicking;
import com.ruoyi.domain.production.MesProductionPickingDetail;
import com.ruoyi.mapper.production.MesProductionPickingMapper;
import com.ruoyi.mapper.production.MesProductionPickingDetailMapper;
import com.ruoyi.service.production.IMesProductionPickingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 生产领料单Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class MesProductionPickingServiceImpl implements IMesProductionPickingService
{
    @Autowired
    private MesProductionPickingMapper mesProductionPickingMapper;

    @Autowired
    private MesProductionPickingDetailMapper mesProductionPickingDetailMapper;

    /**
     * 查询生产领料单
     * 
     * @param pickingId 生产领料单主键
     * @return 生产领料单
     */
    @Override
    public MesProductionPicking selectMesProductionPickingByPickingId(Long pickingId)
    {
        MesProductionPicking picking = mesProductionPickingMapper.selectMesProductionPickingByPickingId(pickingId);
        if (picking != null) {
            // 查询明细列表
            List<MesProductionPickingDetail> detailList = mesProductionPickingDetailMapper.selectDetailListByPickingId(pickingId);
            picking.setPickingDetailList(detailList);
        }
        return picking;
    }

    /**
     * 查询生产领料单列表
     * 
     * @param mesProductionPicking 生产领料单
     * @return 生产领料单
     */
    @Override
    public List<MesProductionPicking> selectMesProductionPickingList(MesProductionPicking mesProductionPicking)
    {
        return mesProductionPickingMapper.selectMesProductionPickingList(mesProductionPicking);
    }

    /**
     * 校验领料单编号是否唯一
     * 
     * @param pickingCode 领料单编号
     * @param pickingId 领料单ID
     * @return 结果
     */
    @Override
    public boolean checkPickingCodeUnique(String pickingCode, Long pickingId)
    {
        if (StringUtils.isEmpty(pickingCode))
        {
            return false;
        }
        MesProductionPicking picking = mesProductionPickingMapper.checkPickingCodeUnique(pickingCode, pickingId);
        return picking == null;
    }

    /**
     * 根据工单ID查询领料单列表
     * 
     * @param workOrderId 工单ID
     * @return 领料单集合
     */
    @Override
    public List<MesProductionPicking> selectPickingListByWorkOrderId(Long workOrderId)
    {
        return mesProductionPickingMapper.selectPickingListByWorkOrderId(workOrderId);
    }

    /**
     * 根据仓库ID查询领料单列表
     * 
     * @param warehouseId 仓库ID
     * @return 领料单集合
     */
    @Override
    public List<MesProductionPicking> selectPickingListByWarehouseId(Long warehouseId)
    {
        return mesProductionPickingMapper.selectPickingListByWarehouseId(warehouseId);
    }

    /**
     * 新增生产领料单
     * 
     * @param mesProductionPicking 生产领料单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMesProductionPicking(MesProductionPicking mesProductionPicking)
    {
        int result = mesProductionPickingMapper.insertMesProductionPicking(mesProductionPicking);
        // 新增明细
        if (mesProductionPicking.getPickingDetailList() != null && !mesProductionPicking.getPickingDetailList().isEmpty()) {
            for (MesProductionPickingDetail detail : mesProductionPicking.getPickingDetailList()) {
                detail.setPickingId(mesProductionPicking.getPickingId());
                mesProductionPickingDetailMapper.insertMesProductionPickingDetail(detail);
            }
        }
        return result;
    }

    /**
     * 修改生产领料单
     * 
     * @param mesProductionPicking 生产领料单
     * @return 结果
     */
    @Override
    @Transactional
    public int updateMesProductionPicking(MesProductionPicking mesProductionPicking)
    {
        // 删除原有明细
        mesProductionPickingDetailMapper.deleteMesProductionPickingDetailByPickingId(mesProductionPicking.getPickingId());
        
        // 新增明细
        if (mesProductionPicking.getPickingDetailList() != null && !mesProductionPicking.getPickingDetailList().isEmpty()) {
            for (MesProductionPickingDetail detail : mesProductionPicking.getPickingDetailList()) {
                detail.setPickingId(mesProductionPicking.getPickingId());
                mesProductionPickingDetailMapper.insertMesProductionPickingDetail(detail);
            }
        }
        
        return mesProductionPickingMapper.updateMesProductionPicking(mesProductionPicking);
    }

    /**
     * 批量删除生产领料单
     * 
     * @param pickingIds 需要删除的生产领料单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteMesProductionPickingByPickingIds(Long[] pickingIds)
    {
        // 删除明细
        for (Long pickingId : pickingIds) {
            mesProductionPickingDetailMapper.deleteMesProductionPickingDetailByPickingId(pickingId);
        }
        return mesProductionPickingMapper.deleteMesProductionPickingByPickingIds(pickingIds);
    }

    /**
     * 删除生产领料单信息
     * 
     * @param pickingId 生产领料单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteMesProductionPickingByPickingId(Long pickingId)
    {
        // 删除明细
        mesProductionPickingDetailMapper.deleteMesProductionPickingDetailByPickingId(pickingId);
        return mesProductionPickingMapper.deleteMesProductionPickingByPickingId(pickingId);
    }

    /**
     * 审核生产领料单
     * 
     * @param pickingId 领料单ID
     * @param status 审核状态
     * @return 结果
     */
    @Override
    public int auditMesProductionPicking(Long pickingId, String status)
    {
        MesProductionPicking picking = new MesProductionPicking();
        picking.setPickingId(pickingId);
        picking.setStatus(status);
        return mesProductionPickingMapper.updateMesProductionPicking(picking);
    }

    /**
     * 执行生产领料
     * 
     * @param pickingId 领料单ID
     * @return 结果
     */
    @Override
    @Transactional
    public int executeMesProductionPicking(Long pickingId)
    {
        // 更新领料单状态为已执行
        MesProductionPicking picking = new MesProductionPicking();
        picking.setPickingId(pickingId);
        picking.setStatus("3"); // 已执行
        
        // TODO: 这里可以添加库存扣减逻辑
        // 1. 查询领料明细
        // 2. 扣减库存
        // 3. 生成库存流水记录
        
        return mesProductionPickingMapper.updateMesProductionPicking(picking);
    }
}